//
//  ViewController.m
//  地图与定位
//
//  Created by wukeng on 16/4/2.
//  Copyright © 2016年 wukeng. All rights reserved.
//

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
#import "Place.h"

@interface ViewController ()<CLLocationManagerDelegate>
{
    CLLocationManager *locationManager;
    
    CLLocation *previousPoint;
}


@property (nonatomic,assign) CLLocationDistance totalMovementDistance;

@property (weak, nonatomic) IBOutlet UILabel *longitudeLabel;

@property (weak, nonatomic) IBOutlet UILabel *latitudeLabel;

@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityView;

@property (weak, nonatomic) IBOutlet MKMapView *myMapView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}
- (IBAction)btnPressed:(id)sender {
    //1.创建位置管理器
    locationManager = [[CLLocationManager alloc] init];
    //2.设置代理
    locationManager.delegate = self;
    [self.activityView startAnimating];
    //3.设置精度
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    //4.设置距离筛选器 既在你移动了一定距离后位置管理器才把监测到的位置更改通知你
    locationManager.distanceFilter = 100.;
    //5.设置定位服务的权限
    [locationManager requestWhenInUseAuthorization];
    //6.启动位置管理器
    [locationManager startUpdatingLocation];
    
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    CLLocation *newLoction = [locations lastObject];
    self.longitudeLabel.text = [NSString stringWithFormat:@"%.5f",newLoction.coordinate.longitude ];
    self.latitudeLabel.text = [NSString stringWithFormat:@"%.5f",newLoction.coordinate.latitude ];
    [self.activityView stopAnimating];
    
    // 将移动路线展示到地图上
//    if (previousPoint == nil) {
//        self.totalMovementDistance = 0;
        Place *start = [[Place alloc] init];
        
        start.coordinate = newLoction.coordinate;
        
        [self.myMapView addAnnotation:start];
        
        MKCoordinateRegion regin = MKCoordinateRegionMakeWithDistance(newLoction.coordinate, 100, 100);
        
        [self.myMapView setRegion:regin animated:YES];
    //}
//    else{
//        self.totalMovementDistance += [newLoction distanceFromLocation:previousPoint];
//    }
//    
    previousPoint = newLoction;
    
    
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    MKPinAnnotationView *pinView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"Anntotaion"];
    if(pinView == nil)
    {
        pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Anntotaion"];
    }
    
    pinView.pinTintColor = [UIColor blueColor];
    //    pinView.pinColor = MKPinAnnotationColorGreen;
    pinView.animatesDrop = YES;
    pinView.canShowCallout = YES;//显示弹出信息
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    btn.tag = 101;
    [btn addTarget:self action:@selector(pinBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
    pinView.rightCalloutAccessoryView = btn;
    
    return pinView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end













